﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Forest.Cat.Domain.Entities;
using Forest.Core.Data;
using Forest.Core.Caching;
using Forest.Core.Events;
using Forest.Core;
using Forest.Cat.Domain.Shared;

namespace Forest.Cat.Domain.Services.Catalog {
    public class JobPostingService : IJobPostingService {
        #region Constants
        private const string JOBPOSTING_BY_ID_KEY = "jobposting.id-{0}";
        private const string JOBPOSTING_PATTERN_KEY = "jobposting.";
        #endregion

        #region Fields
        private readonly IRepository<JobPosting> _jobpostingRepository;
        private readonly ICacheManager _cacheManager;
        private readonly IEventPublisher _eventPublisher;
        #endregion

        #region Ctor
        public JobPostingService(IRepository<JobPosting> jobpostingRepository, ICacheManager cacheManager, IEventPublisher eventPublisher) {
            _jobpostingRepository = jobpostingRepository;
            _cacheManager = cacheManager;
            _eventPublisher = eventPublisher;
        }
        #endregion

        #region Base Methods
        public virtual void DeleteJobPosting(JobPosting jobposting) {
            if (jobposting == null)
                throw new ArgumentNullException("JobPosting");

            _jobpostingRepository.Delete(jobposting);
            _cacheManager.RemoveByPattern(JOBPOSTING_PATTERN_KEY);
            _eventPublisher.EntityDeleted(jobposting);
        }

        public JobPosting GetJobPostingById(Guid jobpostingId) {
            if (jobpostingId == Guid.Empty)
                return null;
            string key = string.Format(JOBPOSTING_BY_ID_KEY, jobpostingId);

            return _cacheManager.Get(key, () => {
                var cl = _jobpostingRepository.GetById(jobpostingId);
                return cl;
            });
        }

        public virtual IPagedList<JobPosting> GetAllJobPostings(int pageIndex, int pageSize) {
            var query = _jobpostingRepository.Table;
            query = query.OrderBy(w => w.Id);
            var items = new PagedList<JobPosting>(query, pageIndex, pageSize);
            return items;
        }

        public virtual void InsertJobPosting(JobPosting jobposting) {
            if (jobposting == null)
                throw new ArgumentNullException("JobPosting");
            _jobpostingRepository.Insert(jobposting);
            _cacheManager.RemoveByPattern(JOBPOSTING_PATTERN_KEY);
            _eventPublisher.EntityInserted(jobposting);
        }

        public virtual void UpdateJobPosting(JobPosting jobposting) {
            if (jobposting == null)
                throw new ArgumentNullException("JobPosting");
            _jobpostingRepository.Update(jobposting);
            _cacheManager.RemoveByPattern(JOBPOSTING_PATTERN_KEY);
            _eventPublisher.EntityUpdated(jobposting);
        }
        #endregion

        #region Extend Methods
        public IList<JobPosting> GetHotJobPosting() {
            var query = from p in _jobpostingRepository.Table
                        where (p.IsHot == true)
                        orderby p.Reposted
                        select p;

            var jobs = query.ToList();
            return jobs;
        }
        #endregion

      
    }
}
